package com.bz.service.impl;

import com.bz.db.enties.BusRoomUser;
import com.bz.db.mapper.BusRoomUserMapper;
import com.bz.service.IBusRoomUserService;
import com.bz.util.UuidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 用户房间表 服务实现类
 *
 * @author wt
 * @since 2024-12-11 16:25
 */
@Slf4j
@Service
public class BusRoomUserServiceImpl implements IBusRoomUserService {

    @Resource
    private BusRoomUserMapper busRoomUserMapper;


    @Override
    public BusRoomUser createRoom(BusRoomUser busRoomUser) {
        BusRoomUser busRoomUser1;
        String roomId = "";
        try {
            List<BusRoomUser> list = busRoomUserMapper.selectByExample(busRoomUser);
            if (CollectionUtils.isEmpty(list)) {
                roomId = UuidUtils.uuid();
                busRoomUser.setRoomid(roomId);
                busRoomUserMapper.insert(busRoomUser);
                busRoomUser1 = busRoomUser;
            } else {
                busRoomUser1 = list.get(0);
                roomId = busRoomUser1.getRoomid();
            }
            // 这里可以添加创建房间的逻辑
            log.info("Creating room with name: {}", roomId);
            // 假设房间创建成功
            return busRoomUser1;
        } catch (Exception e) {
            log.error("Failed to create room", e);
            return null;
        }
    }

    @Override
    public List<BusRoomUser> selectRoomS(BusRoomUser example) {
        List<BusRoomUser> list = busRoomUserMapper.selectRoomS(example);
        return list;
    }

    @Override
    public BusRoomUser selectByPrimaryKey(String roomid) {
        return busRoomUserMapper.selectByPrimaryKey(roomid);
    }
}
